<template>
  <el-card class="box-card">
    <el-form :inline="true" :model="form">
      <el-form-item label="短信类型">
        <el-select v-model="form.type" placeholder="请选择短信类型">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开启状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择开启状态">
          <el-option label="全部" value="" />
          <el-option label="关闭" value="N" />
          <el-option label="开启" value="Y" />
        </el-select>
      </el-form-item>
      <el-form-item label="模板编码">
        <el-input v-model="form.code" placeholder="请输入模板编码" />
      </el-form-item>
      <el-form-item label="短信 API 的模板编号">
        <el-input v-model="form.apiTemplateId" placeholder="请输入短信 API 的模板编号" />
      </el-form-item>
      <el-form-item label="短信渠道">
        <el-select v-model="form.channelId" placeholder="请选择短信渠道">
          <el-option
            v-for="item in channelList"
            :key="item.id"
            :value="item.id"
            :label="item.signature"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" style="margin-top: 10px">
        <el-date-picker
          v-model="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item style="float: right; margin-top: 10px">
        <el-button type="success" v-if="$formatFR(template.add)" @click="handleAdd">新增</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格部分 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column label="模板编码" align="center" prop="code" />
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="模板内容" align="center" prop="content" width="300" />
      <el-table-column label="短信类型" align="center" prop="type">
        <template v-slot="scope">
          {{ typeComputed(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column label="开启状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status == 'Y'">开启</el-tag>
          <el-tag type="info" v-if="scope.row.status == 'N'">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="短信 API 的模板编号"
        align="center"
        prop="apiTemplateId"
        width="180"
      />
      <el-table-column label="短信渠道" align="center" width="120">
        <template v-slot="scope">
          <div>{{ channelComputed(scope.row.channelId) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="mini"
            v-if="$formatFR(template.test)"
            plain
            @click="handleTest(scope.row)"
          >
            测试
          </el-button>
          <el-button
            type="warning"
            size="mini"
            v-if="$formatFR(template.edit)"
            plain
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            v-if="$formatFR(template.del)"
            plain
            @click="handleDel(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :size.sync="form.size"
      :current.sync="form.current"
      :total="total"
      @pagination="getList"
    />
    <!-- 弹窗角色 -->
    <AddEditDialog
      ref="AddEditDialog"
      @updatePage="reset"
      :typeList="typeList"
      :channelList="channelList"
    />
    <TestDialog ref="TestDialog" />
  </el-card>
</template>

<script>
import { page, del } from "@/api/message/sms/template"
import { simplePage } from "@/api/message/sms/channel"
import AddEditDialog from "./components/AddEditDialog"
import FUNCROLELIST from "@/utils/func_role/message"
import TestDialog from "./components/TestDialog"
export default {
  components: {
    AddEditDialog,
    TestDialog
  },
  data() {
    return {
      typeList: [
        {
          label: "验证码",
          value: "1"
        },
        {
          label: "通知",
          value: "2"
        },
        {
          label: "营销",
          value: "3"
        }
      ],
      template: FUNCROLELIST.sms.template,
      form: {
        size: 10,
        current: 1
      },
      date: [],
      total: 0,
      tableData: [],
      channelList: []
    }
  },
  mounted() {
    this.getList()
    this.simplePage()
  },
  computed: {
    typeComputed() {
      return function (v) {
        return this.typeList.find((item) => item.value == v)?.label
      }
    },
    channelComputed() {
      return function (v) {
        return this.channelList.find((item) => item.id == v)?.signature
      }
    }
  },
  methods: {
    reset() {
      this.date = []
      this.form = {
        size: 10,
        current: 1
      }
      this.getList()
    },
    handleAdd() {
      this.$refs.AddEditDialog.init(null, "新增")
    },
    handleTest(row) {
      this.$refs.TestDialog.init(row)
    },
    handleDel(id) {
      this.$confirm("请确认是否删除当前内容?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        del(id).then(() => {
          this.getList()
          this.$message({
            type: "success",
            message: "删除成功!"
          })
        })
      })
    },
    handleUpdate(scope) {
      this.$refs.AddEditDialog.init(scope, "编辑")
    },
    search() {
      this.form.current = 1
      this.getList()
    },

    async getList() {
      if (this.date.length) {
        this.form.startTime = this.date[0]
        this.form.endTime = this.date[1]
      }
      const { data } = await page(this.form)
      this.tableData = data.records
      this.total = data.total
    },
    simplePage() {
      simplePage().then((res) => {
        this.channelList = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dropdown-item-p {
  display: inline;
}
.dropdown {
  margin-left: 10px;
}
</style>
